package com.yc.snack.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import com.yc.snack.biz.IProductInfoBiz;
import com.yc.snack.biz.impl.ProductInfoBizImpl;
import com.yc.snack.entity.ProductInfo;
import com.yc.snack.util.RequestParamUtil;

@WebServlet("/product/*")
public class GoodsInfoController extends BaseController {

	private static final long serialVersionUID = 4229063951458288253L;
	/**
	 * 处理富文本编辑器中图片上传
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	protected void upload(HttpServletRequest request, HttpServletResponse response) throws Exception{
		// 第一个参数：servlet对象，第二个参数：请求对象、第三个参数：响应对选哪个，第四个参数是：错误页面地址、第五个参数是：是否需要session、第六个参数是：缓存大小、第七个参数是：时候自动刷新缓存
		
		PageContext pageContext = JspFactory.getDefaultFactory().getPageContext(this,request, response, null, true, 8192, true);
		Map<String, String> map = RequestParamUtil.upload(pageContext);//返回是文件在服务器中存放地址
		String path = map.get("upload");//注意：ckeditor中的文件上传的文本框的name="upload"
		
		//注意富文本编辑器ckeditor图片上传要求的返回值是一个map对象，里面包含fileName:文件名称, url：文件在服务器中的地址uploaded:回调函数
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("fileName",path.substring(path.lastIndexOf("_") + 1));
		result.put("url", "../../../" + path);
		result.put("uploaded", "1");
		this.send(response, result);
		return;
	}
	/**
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	protected void add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		// 第一个参数：servlet对象，第二个参数：请求对象、第三个参数：响应对选哪个，第四个参数是：错误页面地址、第五个参数是：是否需要session、第六个参数是：缓存大小、第七个参数是：时候自动刷新缓存
		
		PageContext pageContext = JspFactory.getDefaultFactory().getPageContext(this,request, response, null, true, 8192, true);
		ProductInfo pf = RequestParamUtil.upload(ProductInfo.class, pageContext);
		
		//业务模型层
		IProductInfoBiz productInfoBiz = new ProductInfoBizImpl();
		int result = productInfoBiz.add(pf);
		//响应
		if(result > 0) {
			this.send(response,200,"成功");
			return;
		}
		this.send(response, 500,"失败");
		return;
	}
	/**
	 * 第一次查询
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws Exception
	 */
	protected void findFirst(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));

		IProductInfoBiz productInfoBiz = new ProductInfoBizImpl();
		Map<String, Object> map = productInfoBiz.findByFirst(page, rows);
		if(map == null || map.isEmpty()) {
			this.send(response, 600 ,"暂无数据");
			return;
		}
		this.send(response, 200,map);
	}
	protected void finds(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));

		IProductInfoBiz productInfoBiz = new ProductInfoBizImpl();
		List<ProductInfo> list = productInfoBiz.finds(page, rows);
		if(list == null || list.isEmpty()) {
			this.send(response, 600 ,"暂无数据");
			return;
		}
		this.send(response, 200,list);
	}
}
